package academic.ui.client.widgets;

import java.util.ArrayList;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.util.ClientHelper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

import ecommerce.soco.shared.GWT_Product;

public class TodayDealsGraph extends Composite {

	private GenericContainerTag div = new GenericContainerTag("div");
	private GenericContainerTag items = new GenericContainerTag("div");
	private GenericClickable back = new GenericClickable("a");
	private GenericClickable next = new GenericClickable("a");
	private GenericContainerTag wizard = new GenericContainerTag("div");
	private ArrayList<HasClickHandlers> productImageClick=new ArrayList<HasClickHandlers>();
	
	
	
	public TodayDealsGraph() {
		div.setStyleName("recentDealsSlide");
		div.add(back);
		div.add(wizard);
		wizard.add(items);
		wizard.setStyleName("tooltipWizard");
		
		div.add(next);
		back.setVisible(false);
		next.setVisible(false);
		back.setStyleName("prev");
		next.setStyleName("next");
		items.setStyleName("items");
		
		
		
		initWidget(div);
	}

	public void setup(ArrayList<GWT_Product> products) {
		items.clear();
		productImageClick.clear();
		for(int i=0; i<products.size();i++) {
			GenericContainerTag page = new GenericContainerTag("div");
			page.setStyleName("page");
			items.add(page);
			
			final Image img = new Image(GWT.getModuleBaseURL()+"../PICS/Merchants/"+products.get(i).getMerchant().getImage());
			img.getElement().setId(String.valueOf(products.get(i).getId()));
			productImageClick.add(img);
			img.setSize("100px","100px");
			GenericContainerTag productHolder = new GenericContainerTag("div");
			productHolder.add(new BoughtPriceDiv(products.get(i)));
			productHolder.add(img);
			page.add(productHolder);
			
			GenericContainerTag dealInfoSpan = new GenericContainerTag("span",products.get(i).getName());
			dealInfoSpan.setStyleName("productNameInlineDiv");
			
			page.add(dealInfoSpan);
			GenericClickable buyNowButton = new GenericClickable("a");
			buyNowButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					DomEvent.fireNativeEvent(Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false), img);
				}
				
			});
			GenericContainerTag buySpan = new GenericContainerTag("span","buy now");
			buyNowButton.setStyleName("submitButtonLink");
			buySpan.setStyleName("submitButton");
			buyNowButton.add(buySpan);
			page.add(buyNowButton);
		}
	}
	
	public ArrayList<HasClickHandlers> productImageClickables() {
		return productImageClick;
	}
}
